package com.resou.reader.tinder;

import android.os.Build;
import android.text.Html;
import android.util.Log;
import android.widget.TextView;

import com.mindorks.placeholderview.SwipePlaceHolderView;
import com.mindorks.placeholderview.annotations.Click;
import com.mindorks.placeholderview.annotations.Layout;
import com.mindorks.placeholderview.annotations.Resolve;
import com.mindorks.placeholderview.annotations.View;
import com.mindorks.placeholderview.annotations.swipe.SwipeCancelState;
import com.mindorks.placeholderview.annotations.swipe.SwipeIn;
import com.mindorks.placeholderview.annotations.swipe.SwipeInState;
import com.mindorks.placeholderview.annotations.swipe.SwipeOut;
import com.mindorks.placeholderview.annotations.swipe.SwipeOutState;
import com.resou.reader.R;
import com.resou.reader.data.tinder.tinder.SlideBean;
import com.resou.reader.utils.EncryptUtils;

@Layout(R.layout.book_card)
public class BookCard {
    private static final String TAG = "BookCard";
    SwipePlaceHolderView mSwipeView;

    @View(R.id.tv_content)
    TextView tvContent;
    @View(R.id.tv_chapter_name)
    TextView tvChapterName;

    private SlideBean mSlideBean;
    private onCardInteractionListener mListener;

    public BookCard(final SwipePlaceHolderView swipeView, SlideBean slideBean) {
        mSwipeView = swipeView;
        mSlideBean = slideBean;
    }

    public BookCard setInteractionListener(onCardInteractionListener listener) {
        mListener = listener;
        return this;
    }

    @Click(R.id.card_view)
    public void onClick() {
        mListener.onCardClicked(mSlideBean);
    }

    /**
     * 当卡片填充并放入堆栈时，会调用@Resolve。
     * 因此，当堆叠中显示的卡片数量超过1并且顶部卡片被滑走时，下部卡片将成为堆叠的头部，但不会调用@Resolve。
     */
    @Resolve
    void onResolved() {
        tvChapterName.setText(mSlideBean.getChapterName());
        try {
            String decrypt = EncryptUtils.decrypt(mSlideBean.getContent(), mSlideBean.getBookId(), mSlideBean.getChapterId());
            String content = "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;" + decrypt;
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
                tvContent.setText(Html.fromHtml(content, Html.FROM_HTML_MODE_COMPACT));
            } else {
                tvContent.setText(Html.fromHtml(content));
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
//        mListener.onCardSwitched(mSlideBean);
    }

    /**
     * 项目视图移动到左/上侧时调用它。
     */
    @SwipeOut
    void onSwipedOut() {
        mListener.onLeftSwiped(mSlideBean);
//        Toast.makeText(mSwipeView.getContext(), "不再提示", Toast.LENGTH_SHORT).show();
        Log.d("EVENT", "onSwipedOut");
    }

    /**
     * 项目视图移动到右侧/底侧时调用它。
     */
    @SwipeIn
    void onSwipeIn() {
        mListener.onRightSwiped(mSlideBean);
//        Toast.makeText(mSwipeView.getContext(), "标记为喜欢", Toast.LENGTH_SHORT).show();
    }

    /**
     * 当项目视图沿左/上方向移动时，它会被重复调用。
     */
    @SwipeOutState
    void onSwipeOutState() {
        Log.d("EVENT", "onSwipeOutState");
    }

    /**
     * 当项目视图沿右/下方向移动时，会重复调用它。
     */
    @SwipeInState
    void onSwipeInState() {
        Log.d("EVENT", "onSwipeInState");
    }

    /**
     * 在项目视图未被滑入或滑出时调用它。
     * 它被恢复到堆栈中。
     */
    @SwipeCancelState
    void onSwipeCancelState() {
        Log.d("EVENT", "onSwipeCancelState");
    }

    public interface onCardInteractionListener {
        void onLeftSwiped(SlideBean slideBean);

        void onRightSwiped(SlideBean slideBean);

        void onCardClicked(SlideBean slideBean);
    }
}
